﻿import QtQuick 2.0

// 处理鼠标交互的各种计算逻辑，放在CustomChart里面的组件，请不要单独使用
// 变量都直接从父类里面拿了

MouseArea{

    id: root
    property bool isHovered: false
    property bool isPressed: false
    hoverEnabled: true          // 啊……不加hoverEnabled，mouse的位置还不好使

    onEntered: {
        isHovered = true
    }onExited: {
        isHovered = false
    }
    onPressed: {
        isPressed = true
    }onReleased: {
        isPressed = false
    }


    // 实时计算当前鼠标所在位置的真实xy坐标，至于传到哪不知道
    onPositionChanged: {
        if(showDetails === 0){      // 适配能谱图，缩略图下不操作
            return
        }

        var mousePos = mapToItem(this, mouseX, mouseY)

        let dX = mousePos.x / width
        let dY = 1 - (mousePos.y / height)

        let coorX = mousePos.x / width * (maxX - minX) + minX
        let coorY = (1 - mousePos.y / height) * (maxY - minY) + minY
//        console.log("当前鼠标划过的坐标", coorX, coorY)

        // 去找和鼠标所在点最接近的线
        if(mouseHighlightCurve){
            let point = cChartInterface.findPointInScope_byScalePosition(dX, dY)
//            console.log("最近的点", point)
            canvas_mark.coorX = point.x
            canvas_mark.coorY = point.y
            canvas_mark.requestPaint()
            rect_coor.x = mousePos.x - 10*dp
            rect_coor.y = mousePos.y + 10*dp
//            text_coor.text ="(" + (point.x*(maxX-minX)+minX).toFixed(decimalX+1) + ", " + (point.y*(maxY-minY)+minY).toFixed(decimalY+1) + ")"
            text_coor.text = "时间: " + funcTools.second2Time((point.x*(maxX-minX)+minX)) + "\n" + "cps: " + (point.y*(maxY-minY)+minY).toFixed(decimalY)
         }

        // 去找和鼠标所在点最接近的点
        if(mouseHighlightPoint){

        }

        if(isPressed && mouseDrag){

        }



    }

    onWheel: {

        if(showDetails === 0){      // 适配能谱图，缩略图下不操作
            return
        }

        var movLength = wheel.angleDelta.y
        var showRange = cChartInterface.getShowRange()
        var x1 = showRange[0]
        var x2 = showRange[1]
        var y1 = showRange[2]
        var y2 = showRange[3]
        // 找到鼠标点，保证缩放后该点的位置不变
        var mousePos = mapToItem(canvas, mouseX, mouseY)
        var dx = mousePos.x / canvas.width
        var dy = mousePos.y / canvas.height
//            console.log("mousePos", mousePos, "canvasSize:", canvas.height, canvas.width, "DX:", dx, " DY:", dy)

        // 滚轮控制横向缩放
        if(mouseWheelXControl === 0){

            var xGap = x2 - x1
            var xEnlargeLength = - 0.001 * movLength  * (0.2+xGap*0.8)               // gap越小，滚动越慢
            if(x2-x1+xEnlargeLength < minXRangeSize)  xEnlargeLength = (minXRangeSize+0.01) - (x2-x1)       // 这个0.01是防止这个代码给我求极限的
            if(x2-x1+xEnlargeLength > 1)    xEnlargeLength = (1+0.01) - x2 + x1
//                console.log("xEnlargeLength", xEnlargeLength)

            x1 -= xEnlargeLength * dx
            x2 += xEnlargeLength * (1-dx)
            x1 = Math.max(0, Math.min(1-minXRangeSize, x1))
            x2 = Math.max(x1+minXRangeSize, Math.min(1, x2))
        }
        // 滚轮控制竖向缩放
        if(mouseWheelYControl === 0){
            var yGap = y2 - y1
            var yEnlargeLength = - 0.001 * movLength * (0.2+yGap*0.8)               // gap越小，滚动越慢
            if(y2-y1+yEnlargeLength < minYRangeSize)  yEnlargeLength = (minYRangeSize+0.01) - (y2-y1)
            if(y2-y1+yEnlargeLength > 1)    yEnlargeLength = (1+0.01) - y2 + y1
//                console.log("yEnlargeLength", yEnlargeLength)

            y1 -= yEnlargeLength * (1-dy)
            y2 += yEnlargeLength * dy
            y1 = Math.max(0, Math.min(1-minYRangeSize, y1))
            y2 = Math.max(y1+minYRangeSize, Math.min(1, y2))
        }
        // 滚轮控制竖向滚动
        if(mouseWheelYControl === 1){
            var yGap = y2 - y1
            var yMovLength = 0.001 * movLength * (0.2+yGap*0.8)      // gap越小，滚动越慢
            y1 = Math.max(0, Math.min(1-yGap, y1+yMovLength))
            y2 = y1 + yGap

        }
        // 滚轮控制横向滚动
        if(mouseWheelXControl === 1){
            var xGap = x2 - x1
            var xMovLength = -0.001 * movLength * (0.2+xGap*0.8)    // gap越小，滚动越慢
            x1 = Math.max(0, Math.min(1-xGap, x1+xMovLength))
            x2 = x1 + xGap
        }


//            console.log(x1, x2, y1, y2)
        if((x1!==showRange[0]) || (x2!==showRange[1]) || (y1!==showRange[2]) || (y2!==showRange[3])){
            cChartInterface.setShowRange(x1, x2, y1, y2)
            cChartInterface.refresh()
        }
        scrollBar_H.r1 = 1-y2
        scrollBar_H.r2 = 1-y1
        scrollBar_V.r1 = x1
        scrollBar_V.r2 = x2

        // 临时项目用：如果只控制了横向缩放，则锁定该缩放
        if(mouseWheelXControl === 0 && mouseWheelYControl !== 0){
            lastXRange = x2-x1
        }

    }

    Canvas{
        id: canvas_mark
        anchors.fill: parent
        property real coorX: 0
        property real coorY: 0
        visible: root.isHovered
        onPaint: {

            let ctx = canvas_mark.getContext("2d")
            ctx.clearRect(0, 0, width, height)

            ctx.beginPath()
            ctx.fillStyle = "#ee8822"
            ctx.arc(coorX*width, (1-coorY)*height, 5*dp, 0, 2*Math.PI)
            ctx.fill()

            ctx.beginPath()
            ctx.fillStyle = "#ee8822"
            ctx.arc(coorX*width, (1-coorY)*height, 5*dp, 0, 2*Math.PI)
            ctx.fill()
        }
    }
    Rectangle{
        id: rect_coor
        color: "#99111111"
        visible: root.isHovered && canvas_mark.coorX >=0
        radius: 3*dp
        height: text_coor.implicitHeight + 5*dp
        width: text_coor.implicitWidth + 10*dp
        CustomText {
            id: text_coor
            anchors.centerIn: parent
            text: "(0, 0)"
            color: "white"
        }
    }
}
